package com.changgou.controller;

import com.changgou.file.FastDFSFile;
import com.changgou.util.FastDFSClient;
import com.changgou.entity.BaseController;
import com.changgou.entity.Result;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@CrossOrigin
public class FileController extends BaseController{
    
    
    /* 功能描述: TODO  文件上传
     * @param: 
     * @return: 
     * @auther: YPK
     * @date: 2019/11/11 17:44
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        FastDFSFile fastDFSFile = new FastDFSFile(
                file.getOriginalFilename(),
                file.getBytes(),
                StringUtils.getFilenameExtension(file.getOriginalFilename())
        );

        String[] uploadResult = FastDFSClient.uploadFile(fastDFSFile);

        // 3、拼接图片的url地址
        String url = FastDFSClient.getUrl();
        System.out.println("url = " + url);
        url = url + "/" + uploadResult[0] + "/" + uploadResult[1];
        return ResultSuccess("上传成功!",url);
    }
}
